package com.google.java.demo;
import java.io.IOException;
import javax.servlet.http.*;

import com.google.inapppayments.jwt.IapService;
import com.google.inapppayments.jwt.InAppJWT;
import com.google.inapppayments.jwt.Request;

@SuppressWarnings("serial")
public class GwdgDemo extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
	  doPost(req, resp);
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String sellerId = getServletContext().getInitParameter("sellerId");
	    String sellerSecret = getServletContext().getInitParameter("sellerSecret");
	    
	    Request request = new Request(req.getParameter("item"),
	        "description",
	        "20.00",
	        Request.Currencies.valueOf("USD"),
	        "example, example");
	    
	    InAppJWT iap = new InAppJWT(request);
	    IapService iapService = new IapService(sellerId, sellerSecret);
	    
	    String iapJWT = iapService.jwtToString(iap);
	    
	    /** Subscriptions code **/
		/*
		Long iat = cal.getTimeInMillis();
	    Long exp = cal.getTimeInMillis() + 6000000L;
	
	    InitialPayment ip = new InitialPayment(
	        req.getParameter("5.00"),
	        req.getParameter("USD"),
	        req.getParameter("prorated"));

	    Recurrence rc = new Recurrence(
	        req.getParameter("0.99"),
	        req.getParameter("USD"),
	        req.getParameter(exp),
	        req.getParameter("monthly"),
	        req.getParameter("6")
	        );

	    SubscriptionRequest subRequest = new SubscriptionRequest(req.getParameter("subscription demo"),
	        req.getParameter("subscriptions"),
	        ip,
	        rc);
	    subRequest.setSellerData(req.getParameter("some seller data"));

		Calendar cal = Calendar.getInstance();
	    
	    RecurrenceJWT iap = new RecurrenceJWT(iat, exp, subRequest);
	    
	    String jwt = iapService.jwtToString(iap);
	    */
	    resp.setContentType("text/plain");
	    resp.getWriter().println(iapJWT);
	    resp.flushBuffer();
	}
}
